#include "EnemyRed.h"


CEnemyRed::CEnemyRed()
{
	count_time_enemy_up = 1;
	_isForestUp = false;
	count_time_enemy_change = 1;
	red_enemy_count = 0;
	index_enemy_forest = -1;
	temp_add_position = 0;
	frame_start_red = GetTickCount();
	tick_per_frame = 1000 / FRAME_RATE;

	//Enemy Status

	_isRight = true;
	_isJump = true;
	_isRightDown = true;
	_isDown = 0;

	//// gun
	//_isbullet = false;
	//_gun = false;
	//_tick_per_gun = 700;
	//_tick_bullet = 300;
	//_bullet_start = GetTickCount();
	//_levelBullet = 2;
}

void CEnemyRed::LoadResource(LPDIRECT3DDEVICE9 d3ddv, LPD3DXSPRITE &SpriteHandler, HINSTANCE hInstance, HWND hWnd)
{

	for (int i = 0; i < MAX_ENEMY_RED; i++)
	{
		_EnemyRed[i] = new CSprite(SpriteHandler, L"resources\\enemy_red.png", 52, 104, 10, 10, D3DCOLOR_XRGB(255, 0, 255));
	}
	_x = -100;
	_y = -100;
	for (int i = 0; i < MAX_ENEMY_RED_FOREST; i++)
	{
		_EnemyRedForest[i] = new CSprite(SpriteHandler, L"resources\\enemy_red_forest2.png", 50, 38, 3, 3, D3DCOLOR_XRGB(255, 0, 255));
	}

	_sprite_bullet = new CSprite(SpriteHandler, L"resources\\bullet.png", 14, 14, 1, 5, D3DCOLOR_XRGB(255, 255, 255));

	ReadPosition("resources\\Enemy_red.txt");
}

 void CEnemyRed::ReadPosition(char *path)
{

	long isize= 0;
	FILE *file = fopen(path, "rb");

	char *binary;
	

	if (!file)
	{
		printf("\nFile nguon co van de");
	}
	else
	{
		fseek(file, 0, SEEK_END);
		isize = ftell(file);
		fseek(file, 0, SEEK_SET);// fseek(file, size, 0);
		binary = new char[isize];

		fread(binary, 1, isize, file);
		fclose(file);

		int i = 0, j = 0;
		int k = 0;
		int sum = 0;

		while (binary[k] != NULL)
		{
			sum = 0;
			j;
			if (binary[k] > 47 && binary[k] < 58)
			{

				while (binary[k + 1] > 47 && binary[k + 1] < 58)
				{
					sum = sum * 10 + binary[k] - 48;
					k++;
				}
				sum = sum * 10 + binary[k] - 48;

				if ( i== 0) EnemyRed_Position[i][j] = sum *2 ;
				else EnemyRed_Position[i][j] = (7 * 32 - sum) * 2 +32;

				if (++j >6)
				{
					i++;
					j = 0;
				}
			}
			k++;
		}
	}

}
//
//void CEnemyRed::EnemyRedGun()
//{
//	float vx = 0, vy = 0;
//	float idx = 0, idy = 0;
//	_gun = true;
//
//	if (!_isJump)
//	{
//		if (_isRight)
//		{
//
//			if (_isDown == 1)
//			{
//				if (_isRightDown)//chia sung xeo xuong
//				{
//					vx = CONTRABULLETVX;
//					vy = -CONTRABULLETVY;
//					idx = 0;
//					idy = -FRAMEWIDTH / 2;
//				}
//			}
//			else
//			{
//				if (_isDown == -1)
//				{
//					if (_isRightDown)   //chia sung xeo len
//					{
//						vx = CONTRABULLETVX;
//						vy = CONTRABULLETVY;
//						idx = 0;
//						idy = FRAMEWIDTH / 2;
//					}
//				
//				}
//				else
//				{
//					if (_isRightDown)//chay ban
//					{
//						vx = CONTRABULLETVX;
//						vy = 0.0f;
//						idx = 0;
//						idy = 0;
//					}
//				}
//			}
//		}
//		else
//		{
//			if (_isDown == 1)
//			{
//				if (_isRightDown)
//				{
//					vx = -CONTRABULLETVX;
//					vy = -CONTRABULLETVY;
//					idx = -FRAMEHEIGHT;
//					idy = -FRAMEWIDTH / 2;
//				}
//				else
//				{
//					vx = -CONTRABULLETVX;
//					vy = 0.0f;
//					idx = -FRAMEHEIGHT;
//					idy = -FRAMEWIDTH / 2;
//				}
//			}
//			else
//			{
//				if (_isDown == -1)
//				{
//					if (_isRightDown)
//					{
//						vx = -CONTRABULLETVX;
//						vy = CONTRABULLETVY;
//						idx = -FRAMEHEIGHT;
//						idy = FRAMEWIDTH / 2;
//					}
//					
//				}
//				
//			}
//		}
//	}
//	else
//	{
//		if (_isRight)
//		{
//			vx = CONTRABULLETVX;
//			vy = 0.0f;
//			idx = -FRAMEHEIGHT / 2;
//			idy = -FRAMEWIDTH / 2;
//		}
//		else
//		{
//			vx = -CONTRABULLETVX;
//			vy = 0.0f;
//			idx = -FRAMEHEIGHT;
//			idy = -FRAMEWIDTH / 2;
//		}
//	}
//
//	_sprite_bullet->Next(_levelBullet, 1);
//
//	int iframe = 0;// = _x - SCREEN_WIDTH / 2;
//	if (_x < SCREEN_WIDTH / 2)
//		iframe = 0;
//	else
//	{
//		if (_x > GAMEWORLDWIDTH - SCREEN_WIDTH / 2)
//			iframe = _x - SCREEN_WIDTH + (GAMEWORLDWIDTH - _x);
//		else
//			iframe = _x - SCREEN_WIDTH / 2;
//	}
//
//	_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, 4000.0f, vx, vy);
//	_listbullet.Add(_bullet);
//}

void CEnemyRed::Update(D3DXVECTOR2 ContraPositon)
{

	//Enemy sniper

	if (ContraPositon.x > EnemyRed_Position[0][red_enemy_count] + SCREEN_WIDTH / 2)
	{
		red_enemy_count++;
		if (red_enemy_count >= 3) 
			red_enemy_count += 2;
		if (red_enemy_count > 6)
			red_enemy_count = 6;
	}

	
	//Enemy Forest
	if (ContraPositon.x > MARK3 && ContraPositon.x < MARK3 +10)
	{
		index_enemy_forest = 3;
		
	}
	if (ContraPositon.x > MARK4 && ContraPositon.x < MARK4 + 10)
	{
		count_time_enemy_up = 1;
		_isForestUp = false;
		count_time_enemy_change = 1;
		
		index_enemy_forest = 4;

	}
	

	DWORD now1 = GetTickCount();

	if (now1 - frame_start_red >= tick_per_frame)
	{
		frame_start_red = now1;
	////enemy forest
		if (ContraPositon.x > MARK3 && ContraPositon.x < MARK4 + SCREEN_WIDTH)
		{
		    count_time_enemy_up++;
		}

		if (count_time_enemy_up > 20)
		{
			count_time_enemy_change++;
			count_time_enemy_up = 1;

			if (count_time_enemy_change % 2 == 0) _isForestUp = true;
			else _isForestUp = false;
		}

		if (count_time_enemy_up % 20 == 0 && _isForestUp == false)
		{
			if (index_enemy_forest == 3) EnemyRed_Position[1][index_enemy_forest] = 290;
			else EnemyRed_Position[1][index_enemy_forest] = 350;

				if (ContraPositon.x <EnemyRed_Position[0][index_enemy_forest])
				{
			
					_EnemyRedForest[index_enemy_forest - 3]->Next(1, 1);

				}
				else _EnemyRedForest[index_enemy_forest - 3]->Next(2, 1);
		}

		if (count_time_enemy_up % 20 == 0 && _isForestUp == true)
		{

			if (index_enemy_forest == 3)
			{
				EnemyRed_Position[1][index_enemy_forest] = 260;
			}
			 else
				 EnemyRed_Position[1][index_enemy_forest] = 320;

			_EnemyRedForest[index_enemy_forest - 3]->Next(0, 1);
		}

		//Compare position contra vs enemy
		if (red_enemy_count == 3) temp_add_position = 15;
		else temp_add_position = 31;
	 
		if ((ContraPositon.y > EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x <  EnemyRed_Position[0][red_enemy_count]))
			_EnemyRed[red_enemy_count]->Next(0, 1);
		else if ((ContraPositon.y == EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x < EnemyRed_Position[0][red_enemy_count]))
			_EnemyRed[red_enemy_count]->Next(2, 1);
			else 
			if ((ContraPositon.y <  EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x < EnemyRed_Position[0][red_enemy_count]))
				_EnemyRed[red_enemy_count]->Next(4, 1);
				else 
				if ((ContraPositon.y <  EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x > EnemyRed_Position[0][red_enemy_count]))
					_EnemyRed[red_enemy_count]->Next(5, 1);
					else
					if ((ContraPositon.y >  EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x > EnemyRed_Position[0][red_enemy_count]))
							_EnemyRed[red_enemy_count]->Next(8, 1);
							else
							if ((ContraPositon.y == EnemyRed_Position[1][red_enemy_count] + temp_add_position) && (ContraPositon.x > EnemyRed_Position[0][red_enemy_count]))
									_EnemyRed[red_enemy_count]->Next(6, 1);

		/*	//Bullet
			if (GetTickCount() - _bullet_start > _tick_bullet)
				{
					_bullet_start = GetTickCount();
					EnemyRedGun();
				}*/
	}

}
void CEnemyRed::Render(D3DXVECTOR2 ContraPositon)
{

	int vpx_red = 0;
	if (vpx_red <= 0) vpx_red = 0;
	
	vpx_red = ContraPositon.x - SCREEN_WIDTH /2;

	if (vpx_red  >= GAMEWORLDWIDTH - SCREEN_WIDTH)
		vpx_red = GAMEWORLDWIDTH - SCREEN_WIDTH;
	DWORD now = GetTickCount();

	Update(ContraPositon);

	//Enemy Red

	_EnemyRed[red_enemy_count]->Render(EnemyRed_Position[0][red_enemy_count], EnemyRed_Position[1][red_enemy_count]);
	

	_EnemyRedForest[index_enemy_forest-3]->Render(EnemyRed_Position[0][index_enemy_forest], EnemyRed_Position[1][index_enemy_forest]);

	//bullet
	/*_listbullet.Draw(_sprite_bullet);
	if (_gun)
	{
		if (now - _gun_start >= _tick_per_gun)
		{
			_gun_start = now;
			_gun = false;
		}
	}*/
}
CEnemyRed::~CEnemyRed()
{
}
